<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>对象的浅拷贝和深拷贝</title>
</head>
<body>
    
</body>
</html>
<script>
    // 对象的引用
    let car = {
        name: '大众',
        color:'blue',
        price:600000,
        Model:['桑塔纳','捷达','辉腾','辉昂']
    }
    let obj = car
    car.price=180000
    console.log(obj,'---obj---');
    console.log(car,'---car---');
    for(let key in car){
        console.log(car[key]);
    }
    // 浅拷贝
    function objCopy(parent,child){// parent,child分别代表两个对象
        for(let key in parent){
            child[key] = parent[key] 
        }
    }
    let obj1 = {
        name:"张三",
        age:22,
        // 如果属性值是数组或对象等引用型数据类型 仍然无法实现浅拷贝
        skill:['唱歌',"跳舞"]
    }
    let obj2 = {
        sex:"男",
        job:"程序员"
    }
    objCopy(obj1,obj2)
    obj.name="王麻子"
    obj1.skill[0]='演戏'
    console.log(obj2);

    // 递归实现浅拷贝
    function objCopy2(parent,child){
        child ? child : {}
        for (let key in parent){
            if(typeof parent[key] == 'object'){
               child[key] = parent[key].constructor == Array ? [] : {}
               objCopy2(parent[key],child[key])
            }else{
                child[key] = parent[key]
            }
        }
    }
    let obj3 = {
        name:"张三",
        age:22,
        skill:['唱',"跳",'rap','篮球',['敲代码']]
    }
    let obj4 = {
        job:"程序员"
    }
    objCopy2(obj3,obj4)
    console.log(obj4,'----递归拷贝-----');
    obj3.skill[0] = '练习两年半'
    console.log(obj3);
    console.log(obj4);
    
    // JSON 实现对象深拷贝
    let jsonOj = '[{"name":"张三"}]'
    let objJson1 = {
        name:"李四",
        age:28,
        skill:['唱歌','跳舞','演戏']
    }
    // let objStr = JSON.stringify(objJson1)//将对象转换成一个字符串并返回
    // let objNew = JSON.parse(objStr)//将字符串转换为对象
    let objNew = JSON.parse(JSON.stringify(objJson1))
    objJson1.skill[0]='弹钢琴'
    objNew.skill[0]='开车'
    console.log(objJson1,'+++++');
    console.log(objNew,'-------');
    //   对象的合并Object.assign() 
    let obj5 = {
        name:"王五"
    }
    let obj6 = {
        job:"搬砖",
        skill:['唱歌','跳舞','演戏']
    }
    Object.assign(obj5,obj6) 
    obj5.job = "敲代码"

    obj5.skill[0] = '开车'
    console.log(obj5,'---');
    console.log(obj6,'+++');
    let res = {
    "status": "200 OK",
    "usr_info": {
        "username":"lihy_online@163.com",
        "age": 28,
        "nikename":"木子李",
        "tel":"152-2888-5771",
        "isVip": true,
        "tags": ["IT", "吉他", "烹饪", "老司机"]
    }
};
console.log(res.usr_info.username);
// this
console.log('-------this--------');
console.log(this);
// window 代表全局，一般省略不写
console.log(window == this);
let name = '这是一个全局变量的name:zhangsan'
let obj7 = {
    name:'lisi',
    seyHi:function(){
        let name = 'wangwu'
        // this 指向它的调用者
        console.log(this.name);
    }
}
obj7.seyHi()
</script>